package cn.jy.careuplusdoctor.main;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.adapter.SickerAdapter;
import cn.jy.careuplusdoctor.base.BaseActivity;
import cn.jy.careuplusdoctor.http.HttpHelper;
import cn.jy.careuplusdoctor.http.HttpHelper.onHttpListener;
import cn.jy.careuplusdoctor.http.HttpUrls;
import cn.jy.careuplusdoctor.injector.IdInject;
import cn.jy.careuplusdoctor.mycenter.MyCenterActiyity;
import cn.jy.careuplusdoctor.response.SickerResponse;
import cn.jy.careuplusdoctor.utils.CheckNewVersion;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarRightAction;
import cn.jy.careuplusdoctor.widgets.XListView;
import cn.jy.careuplusdoctor.widgets.XListView.IXListViewListener;

public class MainActivity extends BaseActivity implements TitleBarRightAction,IXListViewListener {

	@IdInject(id = R.id.edt_search)
	private EditText mEdtSearch;
	@IdInject(id = R.id.lv_sicker)
	private XListView mLvSicker;
	
	private Map<String, String> params;
	//每次首进入判断当前版本，版本过低就更新
	private boolean isupdate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initTitleBar();
		onInJectView();
		
		initParams();
		
		searchSicker();
		if (!isupdate) {
			CheckNewVersion.checkNewVersion(MainActivity.this);
			isupdate = true;
		}
	}

	@Override
	protected void settingTitleBar() {
		// TODO Auto-generated method stub
		super.settingTitleBar();

		getTitleBar().setTitle("所有患者");
		getTitleBar().setRightText("　　···");
		getTitleBar().setRightAction(this);
		getTitleBar().getLeftView().setVisibility(View.GONE);
		getTitleBar().getRightImageView().setVisibility(View.GONE);
	}
	
	@Override
	protected void onAfterInJector() {
		// TODO Auto-generated method stub
		super.onAfterInJector();
		
		mLvSicker.setPullLoadEnable(true);
		mLvSicker.setPullRefreshEnable(true);
		mLvSicker.setXListViewListener(this);
	}
	
	private void initParams()
	{
		params = new HashMap<String, String>();
		params.put("doctorCode", "123");
		params.put("userName", "");
		params.put("rows", "20");
		params.put("page", "1");
	}
	
	private void searchSicker()
	{
		HttpHelper.getInstance().searchSicker(params, new onHttpListener<SickerResponse>() {
			
			@Override
			public void onResponse(SickerResponse data, int code) {
				if (isHttpSuccess(data) && data.getSickerList() !=null) {
					mLvSicker.setAdapter(new SickerAdapter(MainActivity.this, data.getSickerList()));
				}
			}
		});
	}

	@Override
	public void onRightPress(View view) {
		// TODO Auto-generated method stub
		// 更多
		openActivity(MyCenterActiyity.class);
	}

	// 停止加载
	private void onStopLoad() {
		
		mLvSicker.stopRefresh();
		mLvSicker.stopLoadMore();
		mLvSicker.setRefreshTime("刚刚");
	}
	@Override
	public void onRefresh() {
		
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		
	}
}
